export const WSNRTConnect = function (myZCloudID, myZCloudKey) {
	var thiz = this;
	thiz.uid = myZCloudID;
	thiz.key = myZCloudKey;
	thiz.saddr = "api.zhiyun360.com";

	thiz.onConnect = null;
	thiz.onConnectLost = null;
	thiz.onmessageArrive = null; 

	thiz.setIdKey = function (uid, key) {
		thiz.uid = uid;
		thiz.key = key;
	};

	thiz.initZCloud = function (uid, key) {
		thiz.uid = uid;
		thiz.key = key;
	};

	thiz.setServerAddr = function (addr) {
		thiz.saddr = addr.replace('//', '')
		if (thiz.saddr.indexOf(':') > -1) {
			thiz.saddr = thiz.saddr.split(':').filter(item => {
				return item.length > 10 && item
			})[0]
		}
	};

	thiz.disconnect = function () {
		thiz.wsc.close();
	};
	thiz.connect = function () {
		/* uni-app版 */
		thiz.wsc = uni.connectSocket({
			url: "ws://" + thiz.saddr + ":28080",
			success: () => {
			}
		});
		thiz.wsc.onOpen(event => {
			var auth = {
				method: "authenticate",
				uid: thiz.uid,
				key: thiz.key
			};
			var dat = JSON.stringify(auth);
			thiz.wsc.send({ data: dat });
			if (thiz.onConnect) thiz.onConnect();
		});
		thiz.wsc.onMessage(message => {
			try {
				var msg = JSON.parse(message.data);
				if (msg.method && msg.addr && msg.data) {
					if (msg.method == 'message') {
						if (thiz.onmessageArrive) thiz.onmessageArrive(msg.addr, msg.data);
					}
				}
			} catch (err) {
				// console.log("error msg "+err);
			}
		});
		thiz.wsc.onClose(() => {
			if (thiz.onConnectLost) thiz.onConnectLost();
		});
	};
	/* uni-app版 */
	thiz.sendMessage = function (mac, payload) {
		var msg = {
			method: "control",
			addr: mac,
			data: payload
		};
		var dat = JSON.stringify(msg);
		thiz.wsc.send({ data: dat });
	};
};